package com.ruoyi.printer.service.mqtt;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.printer.domain.vo.MessageVo;
import com.ruoyi.printer.domain.vo.PrinterVo;
import com.ruoyi.printer.service.IPrinterOrderService;
import net.dreamlu.iot.mqtt.spring.server.MqttServerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;

/**
 * 消息发送
 * @author yanyi
 */
@Service
public class MqttServerService {
    private static final Logger log = LoggerFactory.getLogger(MqttServerService.class);
    @Autowired
    private MqttServerTemplate mqttServerTemplate;
    @Autowired
    IPrinterOrderService printerOrderService;

    /**
     * 发送通知给订阅默认主题的所有机器
     * @param notice 通知
     * @return true/false
     */
    public boolean publishNotice(String notice) {
        boolean result = mqttServerTemplate.publishAll("/notice/123", notice.getBytes(StandardCharsets.UTF_8));
        log.info("Mqtt publishNotice result:{}", result);
        return result;
    }

    /**
     * 根据消息json发送信息
     * @param vo 消息内容
     * @return true/false
     */
    public boolean publishByMsg(MessageVo vo) {
        boolean result = mqttServerTemplate.publishAll(vo.getTopic(),
                vo.getPayload().getBytes(StandardCharsets.UTF_8));
        log.info("Mqtt publishByMsg result:{}", result);
        return result;
    }

    /**
     * 根据clientId发送信息 TODO 后续移除
     * @param vo 消息内容
     * @return true/false
     */
    public boolean publishByClientId(MessageVo vo) {
        boolean result = mqttServerTemplate.publish(vo.getClientId(), vo.getTopic(),
                vo.getPayload().getBytes(StandardCharsets.UTF_8));
        log.info("Mqtt publishByClientId result:{}", result);
        return result;
    }

    /**
     * 根据消息json发送信息
     * @param vo 消息内容
     * @return true/false
     */
    public boolean printerByMsg(MessageVo vo) {
        boolean result = false;
        try {
            File file = new File("D://test.txt");
            FileInputStream inputStream = new FileInputStream(file);
            PrinterVo fileVo = new PrinterVo();
            fileVo.setOrderId("1");
            fileVo.setFileData(FileUtils.streamToStr(inputStream));
            result = mqttServerTemplate.publishAll(vo.getTopic(),
                    JSON.toJSONString(fileVo).getBytes(StandardCharsets.UTF_8));
            log.info("Mqtt printerByMsg result:{}", result);
            return result;
        } catch (Exception e) {
            return result;
        }
    }

    /**
     * 根据订单id组装消息体
     * @param orderId 订单id
     * @return true/false
     */
    public boolean printerByOrderId(String orderId) {
        boolean result = false;
        try {
            PrinterVo vo = printerOrderService.selectByOrderId(orderId);
            if (vo == null) {
                log.error("printerByOrderId error order not fount,orderId:{}", orderId);
                return result;
            }
            File file = new File(vo.getUploadPath());
            if (!file.exists()) {
                log.error("printerByOrderId error file not fount,orderId:{}", orderId);
                return result;
            }
            FileInputStream inputStream = new FileInputStream(file);
            vo.setFileData(FileUtils.streamToStr(inputStream));
            result = mqttServerTemplate.publishAll("/printer/"+vo.getShopId(),
                    JSON.toJSONString(vo).getBytes(StandardCharsets.UTF_8));
            log.info("Mqtt printerByOrderId result:{}", result);
            return result;
        } catch (Exception e) {
            log.error("printerByOrderId:",e);
            return result;
        }
    }
}
